package com.supermap.dataservice.disaster.dao;


import com.supermap.dubbo.api.disaster.DisasterBase;
import com.supermap.dubbo.api.disaster.entity.Disaster;
import com.supermap.dubbo.api.disaster.entity.DisasterComment;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @Author:langhongshuai
 * @Description:灾情数据访问层
 * @Date:18/1/29 下午2:42
 */
@Mapper
public interface DisasterDao {
    /**
     * 获得用户的所有灾情信息
     * @return
     */
    @Select("select t3.* ,t4.name as cropname from (select t1.*,t2.username from t_disaster t1 \\n\" +\n" +
            "            \"inner JOIN t_user t2 on t1.userId=t2.id) t3\\n\" +\n" +
            "            \"inner JOIN t_crop t4 on t3.type=t4.id where IF(ISNULL(#{userId}),true,t3.userId=#{userId}) and IF(ISNULL(#{areaCode}),true,t3.areacode like #{areaCode})")
    public List<Disaster> getDisasterByUIdArea(@Param("userId") String userId, @Param("areaCode") String areaCode);

    /**
     *增加灾情
     * @param disaster
     */
//    @Insert("insert into t_disaster(userId,farm,type,breed,growthperiod,longitude,latitude,areacode,descript,disastertype,development)\n " +
//            "values(#{disastercloud.userId},#{disastercloud.farm},#{disastercloud.type},#{disastercloud.breed},#{disastercloud.growthperiod},\n" +
//            "#{disastercloud.longitude},#{disastercloud.latitude},#{disastercloud.areacode},#{disastercloud.descript},\n" +
//            "#{disastercloud.disastertype},#{disastercloud.development})")
    @InsertProvider(type =DisasterDaoSQL.class,method = "addDisasterSQL")
    public void addDisaster(@Param("disaster") DisasterBase disaster);
    @Select("select id,name from t_disaster_lev")
    List<Map<String,Object>> getDisasterLev();

    class DisasterDaoSQL{
        public String addDisasterSQL(Map<String,Object> map){
            DisasterBase disaster=(DisasterBase)map.get("disaster");
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("insert into t_disaster ");
            StringBuilder title = new StringBuilder();
            StringBuilder val = new StringBuilder();
            if (disaster.getUserId()!=null) {
                title.append("userId,");
                val.append("" + disaster.getUserId() + ",");
            }
            if (disaster.getFarmlandid() != null) {
                title.append("farm,");
                val.append("" + disaster.getFarmlandid() + ",");
            }
            if (disaster.getType()!=null) {
                title.append("type,");
                val.append("" + disaster.getType() + ",");
            }
            if (disaster.getBreed() != null) {
                title.append("breed,");
                val.append("'" + disaster.getBreed() + "',");
            }
            if (disaster.getGrowthPeriod()!=null) {
                title.append("growthperiod,");
                val.append("" + disaster.getGrowthPeriod() + ",");
            }
            if (disaster.getLatitude() != null) {
                title.append("longitude,");
                val.append("" + disaster.getLongitude() + ",");
            }
            if (disaster.getLatitude()!=null) {
                title.append("latitude,");
                val.append("" + disaster.getLatitude() + ",");
            }
            if (disaster.getAreaCode() != null) {
                title.append("areacode,");
                val.append("'" + disaster.getAreaCode() + "',");
            }
            if (disaster.getDescript()!=null) {
                title.append("descript,");
                val.append("'" + disaster.getDescript() + "',");
            }
            if (disaster.getDisasterType() != null) {
                title.append("disastertype,");
                val.append("'" + disaster.getDisasterType() + "',");
            }
            if (disaster.getDevelopment() != null) {
                title.append("development,");
                val.append("'" + disaster.getDevelopment() + "',");
            }
            if (disaster.getLev() != null) {
                title.append("lev,");
                val.append("" + disaster.getLev() + ",");
            }

            return   stringBuilder.append("(" + title.substring(0, title.lastIndexOf(",")) + ") values(" + val.substring(0, val.lastIndexOf(",")) + ")").toString();

        }
    }
    @Select(" select LAST_INSERT_ID();")
    int  getAddDisasterId();

    @Insert(" insert t_disasterimg(disasterId,imageURLContent) values(#{disasterId},#{imageURLContent})")
    int  AddDisasterImg(@Param("disasterId")int disasterId, @Param("imageURLContent")String imageURLContent);


    /**
     * 获取所有的灾情信息
     * @return
     */
    @Select("select t3.* ,t4.`name` as cropname from (select t1.*,t2.username from t_disaster t1 \n" +
            "Inner JOIN t_user t2 on t1.userId=t2.id) t3\n" +
            "Inner JOIN t_crop t4 on t3.type=t4.id;")
    public List<Disaster> getAllDisaster();

    /**
     * 获取所有的灾情评论
     * @return
     */
    @Select("select * from t_disastercomment")
    public List<DisasterComment> getAllDisasterComment();

    /**
     * 删除灾情
     * @param disId
     */
    @Delete("Delete from t_disaster where id=#{disId}")
    public void delDisaster(@Param("disId") int disId);

    /**
     * 根据区域编码获取所有的灾情信息
     * @return
     */
    @Select("select t3.* ,t4.`name` as cropname,t5.name as disasterName from (select t1.*,t2.username from t_disaster t1 " +
            " Inner JOIN t_user t2 on t1.userId=t2.id) t3 " +
            " Inner JOIN t_crop t4 on t3.type=t4.id " +
            " Inner JOIN t_disastertype t5 on t3.disastertype=t5.code " +
            " where t3.areacode =#{areacode}")

    public List<Disaster> getDisasterByareacode(@Param("areacode") String areacode);

    /**
     * 根据type得到农情
     * @param type
     * @return
     */
    @Select("select t1.*,t2.username as username,t3.areaname as areaname from t_disaster t1 left join t_user t2 on t1.userId = t2.id left join t_area t3 on t3.areacode = t1.areacode where if((isnull(#{type}) or #{type}=''),true,t1.type in(select id from t_crop where id in(select id from t_crop where parentid = #{type} or id= #{type}) or"
            +" parentid in (select id from t_crop where parentid = #{type} ))) and if(isnull(#{areaCode}),true,t1.areacode like CONCAT(#{areaCode},'%'))  and if(isnull(#{disasterType}) or #{disasterType}='',true,t1.disastertype = #{disasterType}) and if(isnull(#{dateTime}),true,t1.createTime like CONCAT(#{dateTime},'%'))")
    List<Disaster> getDisasters(@Param("type") String type, @Param("areaCode") String areaCode, @Param("disasterType") String disasterType, @Param("dateTime") String dateTime);

    /**
     * 搜索
     * @param showName
     * @param areaCode
     * @return
     */
    @Select("select t1.*,t2.username as username,t2.showName,t3.areaname as areaname " +
            "from t_disaster t1 left join t_user t2 on t1.userId = t2.id left join t_area t3 on t3.areacode = t1.areacode where t2.showName like #{showName} \n" +
            "and t2.areaCode like #{areaCode}")
    List<Disaster> searchDisasterByShowName(@Param("showName") String showName, @Param("areaCode") String areaCode);
}
